package com.wx.mall.user.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.common.vo.user.GrowthReqVo;
import com.wx.mall.user.entity.Growth;
import com.wx.mall.user.entity.User;
import com.wx.mall.user.service.IGrowthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 成长值表 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-18
 */
@Api(tags = "成长值管理",description = "GrowthController")
@RestController
@RequestMapping("/growth")
@Slf4j
public class GrowthController {
    @Resource
    IGrowthService service;

    @ApiOperation("根据用户ID来进行查询")
    @GetMapping("/listByUserId")
    public DataResult getListByUserId(Integer growthUserId){
        List<Growth> list=service.getListByUserId(growthUserId);
        return DataResult.success(list);

    };
    @ApiOperation("分页:根据用户ID来进行查询")
    @GetMapping("/listByUserIdPage")
    public CommonResult<CommonPage<Growth>> getListByUserIdPage(Integer pageNum,
                                           Integer pageSize, Integer growthUserId){
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
        List<Growth> list=service.getListByUserIdPage(pageNum,pageSize,growthUserId);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页查询结果

    };
    @ApiOperation("分页:根据用户ID来进行查询并执行筛选操作")
    @GetMapping("/listByUserIdPageSelect")
    public CommonResult<CommonPage<Growth>> getListByUserIdPageSelect(Integer pageNum,
                                                                Integer pageSize, Integer growthUserId, String growthSource, Date growthCreateTime){
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
        List<Growth> list=service.getListByUserIdPageSelect(pageNum,pageSize,growthUserId,growthSource,growthCreateTime);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页查询结果

    };

    @ApiOperation("根据用户ID来进行查询根据成长值来源排序")
    @GetMapping("/listByOder")
    public CommonResult<CommonPage<Growth>> getListByOder(Integer pageNum,
                                    Integer pageSize,Integer growthUserId,Integer oder){
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
//      oder:  0升1降
        List<Growth> list=service.getListByOder(pageNum,pageSize,growthUserId,oder);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页查询结果
    };

    @ApiOperation("根据用户ID来进行查询根据时间排序")
    @GetMapping("/listByTime")
    public CommonResult<CommonPage<Growth>> getListByTime(Integer pageNum,
                                 Integer pageSize,Integer growthUserId,Integer oder){
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
        //      oder:  0升1降s
        List<Growth> list=service.getListByTime(pageNum,pageSize,growthUserId,oder);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页查询结果
    };

    @ApiOperation("插入一条信息,同时修改用户表成长值信息")
    @PostMapping("/addList")
    public DataResult addList(User user, Growth growth){
        System.out.println(user);
        System.out.println(growth);
       int rs= service.addList(user,growth);
        return rs==1?DataResult.success():DataResult.getResult(1,"插入失败");
    };
    @ApiOperation("批量插入")
    @PostMapping("/addManyList")
    public DataResult addManyList(GrowthReqVo vo){
        System.out.println(vo);

        int add=service.addManyList(vo);
        return add==1?DataResult.success():DataResult.getResult(1,"批量插入失败");
    }

}

